﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Ewts.Storage
{
    public partial class FinaceByid : System.Web.UI.Page
    {
        int loadid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                
                if (Request["loadnum"] != null && Request["loadnum"].ToString().Trim() != "")
                    this.tdLoadnum.InnerText = Request["loadnum"].ToString().Trim();
                if (Request["cumcode"] != null && Request["cumcode"].ToString().Trim() != "")
                    this.tdCumCode.InnerText = Request["cumcode"].ToString().Trim();
                if (Request["cumname"] != null && Request["cumname"].ToString().Trim() != "")
                    this.tdCumName.InnerText = Request["cumname"].ToString().Trim();
                if (Request["loadnumid"] != null && Request["loadnumid"].ToString().Trim() != "")
                {
                    loadid = Convert.ToInt32(Request["loadnumid"].ToString().Trim());
                    DataTable dt = new DataLib.BLL.FinaceData().GetList(" LoadNumID=" + loadid).Tables[0];
                    if (dt.Rows.Count>0&&dt!=null)
                    {
                        this.txtCollecInvoiceDate.Text = dt.Rows[0]["CollecInvoiceDate"].ToString();
                        this.txtCollectMoneydate.Text = dt.Rows[0]["CollecMoneyDate"].ToString();
                        this.txtInvoiceDate.Text = dt.Rows[0]["InvoiceDate"].ToString();
                        this.txtPayDate.Text = dt.Rows[0]["PayDate"].ToString();
                    }
                
                }

            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.tdLoadnum.InnerText==""||this.tdLoadnum.InnerText==null)
            {
                DataLib.Common.MessageBox.Show(Page, "请先回到财务数据页面");
                return;
            }
            loadid = Convert.ToInt32(Request["loadnumid"].ToString().Trim());
            List<DataLib.Model.FinaceData> list = new DataLib.BLL.FinaceData().GetModelList(" LoadNumID=" + loadid);
           
            if (list != null&&list.Count>0)
            {
               
                DataLib.Model.FinaceData finaceModel=new DataLib.BLL.FinaceData().GetModelList(" LoadNumID=" + loadid)[0];
                finaceModel.CollecInvoiceDate = this.txtCollecInvoiceDate.Text;
                finaceModel.CollecMoneyDate = this.txtCollectMoneydate.Text;
                finaceModel.InvoiceDate = this.txtInvoiceDate.Text;
                finaceModel.PayDate = this.txtPayDate.Text;
                new DataLib.BLL.FinaceData().Update(finaceModel);
                clearAll();
                DataLib.Common.MessageBox.ShowAndRedirect(Page, "保存成功", "FinaceData.aspx");
            }
            else
            {
                DataLib.Model.FinaceData model = new DataLib.Model.FinaceData();
                model.LoadNumID = loadid;
                model.LoadNumNo = this.tdLoadnum.InnerText;
                model.CustomerCode = this.tdCumCode.InnerText;
                model.CustomerName = this.tdCumName.InnerText;
                model.CollecInvoiceDate = this.txtCollecInvoiceDate.Text;
                model.CollecMoneyDate = this.txtCollectMoneydate.Text;
                model.InvoiceDate = this.txtInvoiceDate.Text;
                model.PayDate = this.txtPayDate.Text;

                DataTable dt= new DataLib.BLL.Cumtomer().GetList(" customercode='" + this.tdCumCode.InnerText + "'").Tables[0];
                if (dt.Rows.Count>0)
                {
                    model.CustomerID =Convert.ToInt32(dt.Rows[0][0]);
                }
                new DataLib.BLL.FinaceData().Add(model);
                clearAll();
                DataLib.Common.MessageBox.ShowAndRedirect(Page, "保存成功", "FinaceData.aspx");
                   
            }
        }

        protected void clearAll()
        {
            this.tdLoadnum.InnerText = "";
            this.tdCumCode.InnerText = "";
            this.tdCumName.InnerText = "";
            this.txtCollecInvoiceDate.Text = "";
            this.txtCollectMoneydate.Text = "";
            this.txtInvoiceDate.Text = "";
            this.txtPayDate.Text = "";
        }
         
    }    
}        
         
         